package com.kandinfo.isbnmanage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.base.common.dto.widget.DataTablesResponseDto;
import com.kandinfo.base.security.SecurityContext;
import com.kandinfo.base.security.util.RoleConstants;
import com.kandinfo.isbnmanage.domian.IsbnManage;
import com.kandinfo.isbnmanage.dto.IsbnDealtReqDto;
import com.kandinfo.isbnmanage.dto.IsbnDealtResDto;
import com.kandinfo.isbnmanage.dto.IsbnManageReqDto;
import com.kandinfo.isbnmanage.dto.IsbnManageResDto;
import com.kandinfo.isbnmanage.repository.IsbnRepository;
import com.kandinfo.isbnmanage.service.IsbnManageService;
import com.kandinfo.isbnmanage.service.IsbnService;



@Service
public class IsbnServiceImpl implements IsbnService {

	@Autowired
	private IsbnRepository isbnRepository;
	
	@Autowired
	private IsbnManageService isbnManageService;
	
	@Override
	public DataTablesResponseDto<IsbnManageResDto> searchByCon(IsbnManageReqDto isbnManageReqDto) {
		DataTablesResponseDto<IsbnManageResDto> responseDto = new DataTablesResponseDto<IsbnManageResDto>();
		responseDto.setEcho(isbnManageReqDto.getEcho());
		responseDto.setData(isbnRepository.searchByCon(isbnManageReqDto));
		responseDto.setTotalRecords(isbnRepository.getIsbnManageCount(isbnManageReqDto));
		responseDto.setTotalDisplayRecords(responseDto.getTotalRecords());
		return responseDto;
	}

	
	@Override
	public DataTablesResponseDto<IsbnDealtResDto> searchIsbnDealtByCon(IsbnDealtReqDto isbnDealtReqDto) {
		if (!SecurityContext.getInstance().hasRoles(RoleConstants.ISBN_MANAGER)) {
			isbnDealtReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		}
		DataTablesResponseDto<IsbnDealtResDto> responseDto = new DataTablesResponseDto<IsbnDealtResDto>();
		responseDto.setEcho(isbnDealtReqDto.getEcho());
		responseDto.setData(isbnRepository.searchIsbnDealtByCon(isbnDealtReqDto));
		responseDto.setTotalRecords(isbnRepository.getIsbnDealtCount(isbnDealtReqDto));
		responseDto.setTotalDisplayRecords(responseDto.getTotalRecords());
		return responseDto;
	}


	@Override
	public ResultDto getIsbnManage(Long isbnManageId) {
		if (isbnManageId == null) {
			return ResultDtoFactory.toNack("isbnManageId为null!");
		}
		IsbnManage isbnManage = isbnManageService.findOne(isbnManageId);
		if (isbnManage == null) {
			return ResultDtoFactory.toNack("查询不到IsbnManage信息!");
		}
		return ResultDtoFactory.toAckNomessage(isbnManage);
	}


	@Override
	public ResultDto getIsbnManageList(IsbnManageReqDto isbnManageReqDto) {
		return ResultDtoFactory.toAckNomessage(isbnRepository.searchByCon(isbnManageReqDto));
	}


}
